package com.jxdinfo.hussar.example.common.schedulingtask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxdinfo.hussar.example.common.schedulingtask.dao.MdItemMapper;
import com.jxdinfo.hussar.example.common.schedulingtask.model.MdClient;
import com.jxdinfo.hussar.example.common.schedulingtask.model.MdItem;
import com.jxdinfo.hussar.example.common.schedulingtask.service.MdItemService;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.MdClientVO;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.MdItemTypeVO;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.MdItemVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 物料产品表 服务实现类
 * </p>
 *
 * @author 乔镔
 * @since 2024-04-02
 */
@Service("com.jxdinfo.hussar.example.common.schedulingtask.service.impl.MdItemServiceImpl")
public class MdItemServiceImpl extends ServiceImpl<MdItemMapper, MdItem> implements MdItemService {

    private static final Logger logger = LoggerFactory.getLogger(MdItemServiceImpl.class);

    @Autowired
    private MdItemMapper mdItemMapper;

    @Autowired
    private MdItemService mdItemService;


    @Override
    public void manualSynchronization() {
        logger.info("\n手动同步-产品表-开始同步产品表");
        LocalDateTime nowTime = LocalDateTime.now();
        // 1查询erp所有的产品表信息
        List<MdItemVO> allitem = mdItemMapper.selectAllItem();
        // 2产品表信息转换成主键-对象map集合
        Map<Long, MdItemVO> allItemMap = allitem.stream().collect(Collectors.toMap(MdItemVO::getMasterId, t -> t));
        // 3产品表信息中的id转换成id列表
        List<Long> erpAllClientIdList = allitem.stream().map(MdItemVO::getMasterId).collect(Collectors.toList());
        // 4查询所有的mes产品表信息
        QueryWrapper<MdItem> mdItemWrapper = new QueryWrapper<>();
        mdItemWrapper.eq("enable_flag","1");
        List<MdItem> mesMdItems = mdItemMapper.selectList(mdItemWrapper);
        // 5mes中的物料产品信息转换成id列表,
        List<Long> mesIdList = mesMdItems.stream().map(MdItem::getErpItemId).collect(Collectors.toList());
        // 6找出erp中有但是mes中没有的id，这写id需要插入
        ArrayList<Long> erpHasMesNoIdList = new ArrayList<>();
        for (int i = 0; i < erpAllClientIdList.size(); i++) {
            if (!mesIdList.contains(erpAllClientIdList.get(i))) {
                erpHasMesNoIdList.add(erpAllClientIdList.get(i));
            }
        }
        if (erpHasMesNoIdList.size() > 0) {
            // 循环创建对象，保存待要插入的结果
            ArrayList<MdItem> insertList = new ArrayList<>();
            for (Long insertId : erpHasMesNoIdList) {
                MdItemVO mdItemVO = allItemMap.get(insertId);
                MdItem mdItem = new MdItem();
                mdItem.setItemId(mdItemVO.getMasterId());
                mdItem.setItemCode(mdItemVO.getItemCode());  // 料号－》产品物料编码
                mdItem.setItemName(mdItemVO.getItemName());            // 品名-》产品物料名称
                mdItem.setSpecification(mdItemVO.getSpecification()); // 规格-》规格型号
                mdItem.setEnableFlag(mdItemVO.getEnableFlag());  // 是否生效
                mdItem.setItemTypeId(mdItemVO.getItemTypeId());  // 物料id
                mdItem.setItemTypeCode(mdItemVO.getItemTypeCode());  // 物料id
                mdItem.setItemTypeName(mdItemVO.getItemTypeName());  // 物料id
                mdItem.setUnitOfMeasure(mdItemVO.getUnitOfMeasure());  // 单位
                mdItem.setMeasureId(mdItemVO.getMeasureId());  // 单位Id
                mdItem.setItemOrProduct("PRODUCT");
                mdItem.setScheduleTime(nowTime);
                mdItem.setStandardGrade(mdItemVO.getStandardGrade());
                mdItem.setStartGrade(mdItemVO.getStartGrade());
                mdItem.setEndGrade(mdItemVO.getEndGrade());
                mdItem.setCreateBy(mdItemVO.getMasterCreatedBy());
                mdItem.setCreateTime(mdItemVO.getMasterCreatedOn());
                mdItem.setUpdateBy(mdItemVO.getMasterModifiedBy());
                mdItem.setUpdateTime(mdItemVO.getMasterModifiedOn());
                mdItem.setErpItemId(mdItemVO.getMasterId());
                insertList.add(mdItem);
            }
            mdItemService.saveBatch(insertList);
        }
        // 7找出erp和mes中都有的，这些需要更新
        ArrayList<Long> updateIdList = new ArrayList<>();
        for (int i = 0; i < mesIdList.size(); i++) {
            if (erpAllClientIdList.contains(mesIdList.get(i))){
                updateIdList.add(mesIdList.get(i));
            }
        }
        if (updateIdList.size()>0) {
            ArrayList<MdItem> mdItemUpdateList = new ArrayList<>();
            List<MdItem> mdItemList = mdItemMapper.selectBatchIds(updateIdList);
            for (MdItem mdItem : mdItemList) {
                Long erpmdItemId = mdItem.getErpItemId();
                MdItemVO mdItemVO = allItemMap.get(erpmdItemId);
                mdItem.setItemCode(mdItemVO.getItemCode());              // 料号－》产品物料编码
                mdItem.setItemName(mdItemVO.getItemName());              // 品名-》物料名称
                mdItem.setSpecification(mdItemVO.getSpecification());    // 规格-》规格型号
                mdItem.setItemTypeId(mdItemVO.getItemTypeId());          // 物料id
                mdItem.setItemTypeCode(mdItemVO.getItemTypeCode());      // 物料code
                mdItem.setItemTypeName(mdItemVO.getItemTypeName());      // 物料name
                mdItem.setEnableFlag(mdItemVO.getEnableFlag());          // 启用状态
                mdItem.setUnitOfMeasure(mdItemVO.getUnitOfMeasure());    //单位
                mdItem.setMeasureId(mdItemVO.getMeasureId());            //单位id
                mdItem.setItemOrProduct("PRODUCT");
                mdItem.setScheduleTime(nowTime);
                mdItem.setStandardGrade(mdItemVO.getStandardGrade());
                mdItem.setStartGrade(mdItemVO.getStartGrade());
                mdItem.setEndGrade(mdItemVO.getEndGrade());
                mdItem.setCreateBy(mdItemVO.getMasterCreatedBy());
                mdItem.setCreateTime(mdItemVO.getMasterCreatedOn());
                mdItem.setUpdateBy(mdItemVO.getMasterModifiedBy());
                mdItem.setUpdateTime(mdItemVO.getMasterModifiedOn());
                mdItemUpdateList.add(mdItem);
            }
            mdItemService.updateBatchById(mdItemUpdateList);
        }
    }
}
